package com.candy.service.impl;

import com.candy.dao.AccountDao;
import com.candy.dao.impl.AccountDaoImpl;
import com.candy.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 使用注解方式进行配置的内容与xml方式配置的内容实现的功能基本上是一致的
 * 完成的配置：“
 *    <bean id="accountService" class="com.candy.service.impl.AccountServiceImpl"
 *           scope="singleton"
 *           init-method="start"
 *           destroy-method="end">
 *         <property name="" value=""/ref=""></property>
 *     </bean>
 *  对xml配置方式进行解析，将配置的功能可以分为以下几类：
 *  1、用于创建对象的
 *      相当于xml配置中的bean标签的作用一致，用于创建对象，并存入IOC容器中
 *   2、用于注入数据的
 *      相当于xml配置中的property标签的作用一致
 *  3、改变作用范围的：
 *       相当于xml配置中scope属性的作用是一致的
 *  4、与生命周期相关 的
 *       相当于xml配置中的init-method、destroy-method属性的作用是一致的
 */
@Service(value = "accountService")
//@Scope("prototype")
//@Service
public class AccountServiceImpl implements AccountService {
    @Value("张三")
    private String name;
    private Integer age;
    private Date date;

    public AccountServiceImpl(String name, Integer age, Date date) {
        this.name = name;
        this.age = age;
        this.date = date;
    }
//    @Autowired
//    @Qualifier(value = "accountDaoImpl")

//    @Resource(name = "accountDaoImpl")
    @Value("#{accountDaoImpl}")
    private AccountDao accountDao;

    public AccountServiceImpl() {
        System.out.println("调用了无参构造函数");
    }

    @Override
    public void save() {
        accountDao.save();
    }
    @PostConstruct
    public void start(){
        System.out.println("初始化调用的我--------start");
    }
    @PreDestroy
    public void end(){
        System.out.println("对象销毁时调用的我--------end");
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }
}
